package com.clas.liu.myapplication.presenter.impl;

import android.os.Handler;
import android.os.Message;

import com.clas.liu.myapplication.model.event.BaseEvent;
import com.clas.liu.myapplication.model.i.IBaseFragmentModel;
import com.clas.liu.myapplication.model.impl.BaseFragmentModel;
import com.clas.liu.myapplication.view.fragment.i.IBaseFragment;
import com.clas.liu.myapplication.view.fragment.impl.BaseFragment;

import java.lang.ref.WeakReference;

/**
 * 所有Fragment对应的Presenter的父类
 * 对持有的Fragment和Model的类型做了严格的限制!
 * 泛型<V extends FragmentBase & IBaseFragment>是其持有的Fragment对象类型
 * 所持有的Fragment都应该继承BaseFragment
 * 所持有的Fragment都应该实现IBaseFragment 的子接口,以便于Presenter通过接口回调Fragment的方法
 * 泛型<M extends BaseFragmentModel & IBaseFragmentModel>是其持有的Model对象
 * 所持有的的Model对象都应该继承BaseFragmentModel
 * 所持有的Model都应该实现IBaseFragmentModel的子接口,以便于Presenter通过接口调用Model的方法
 * Created by liu on 2019/2/28.
 */

public abstract class BaseFragmentPresenter <M extends BaseFragmentModel & IBaseFragmentModel,V extends BaseFragment &IBaseFragment,
        E extends BaseEvent> implements BaseFragmentModel.OnEventReceiveListener<E>{

    V mView;    //Presenter所持有的View对象
    M mModel;   //Presenter所持有的Model对象

    public BaseFragmentPresenter(V view){
        mView = view;
        mModel = getModel(this);
    }

    protected abstract M getModel(BaseFragmentModel.OnEventReceiveListener listener);

    public abstract void eventReceiveOnUiThread(Message msg);
    @Override
    public abstract void eventReceive(E event, int what);

    public void onDestroy() {
        if(mView!=null){
            mView = null;
        }
        if (mModel!=null){
            mModel.onDestroy();
            mModel = null;
        }
    }

}
